package com.myebills.ebpsfrontend.cust.maxis.ms05;

import com.myebills.ebpsfrontend.cust.controller.*;
import com.checkfree.isolutions.customer.persist.Account;
import com.checkfree.isolutions.customer.persist.User;
import com.checkfree.isolutions.admin.ObjectId;
import com.checkfree.isolutions.statement.persist.Statement;
import com.checkfree.isolutions.statement.StatementSection;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CusTelDetServlet_05_v2r00 extends EbppxTransactionServlet {

    /**
     *
     *
     */
    public void doTransaction(HttpServletRequest req, HttpServletResponse res)
    throws ServletException {
        try {
            final HttpSession session = req.getSession(false);
            if (null == session) {
                throw new ServletException("Session must be already established");
            }

    		final String randomKey = (String) session.getAttribute(EbppxConstants.RANDOM_KEY);

            final EbppxUrlManager urlManager = getUrlManager(req);

            final String stmtIndex = urlManager.getSecureParameter("stmtIndex");
            if (stmtIndex.length() == 0) {
                throw new ServletException("Missing statement index");
            }

            final String sOidStatement = urlManager.getSecureParameter(EbppxConstants.STATEMENT_ID);
            if (sOidStatement.length() == 0) {
                throw new ServletException("Missing statement identifier");
            }

            final String sOidAccount   = urlManager.getSecureParameter(EbppxConstants.ACCOUNT_ID);
            if (sOidAccount.length() == 0) {
                throw new ServletException("Missing account identifier");
            }

            final String phoneNo = urlManager.getSecureParameter("phoneNo");
            if (phoneNo.length() == 0) {
                throw new ServletException("Missing phone number");
            }

            final String phoneIndex = urlManager.getSecureParameter("phoneIndex");
            if (phoneIndex.length() == 0) {
                throw new ServletException("Missing phone index");
            }

            final String simNo = urlManager.getSecureParameter("simNo");

            final String sectionKey = urlManager.getSecureParameter("sectionKey");
            if (sectionKey.length() == 0) {
                throw new ServletException("Missing section key");
            }

            String repeatSectionKey = urlManager.getSecureParameter("repeatSectionKey");
            if (repeatSectionKey == null) {
                repeatSectionKey = "";
            }

            String flags = urlManager.getSecureParameter("flags");
            if (flags.length() == 0) {
                flags = "00";
            }

            String searchBy = "";
            String searchVal1 = "";
            String searchVal2 = "";
            String search = urlManager.getRequestParameter("search");
            if (search.length() == 0) {
                searchBy = (String) session.getAttribute("searchBy");
                searchVal1 = (String) session.getAttribute("searchVal1");
                searchVal2 = (String) session.getAttribute("searchVal2");
            } else {
                searchBy = urlManager.getRequestParameter("searchBy");
                if (searchBy.length() == 0) {
                    searchBy = "";
                }
                session.setAttribute("searchBy",searchBy);

                searchVal1 = urlManager.getRequestParameter("searchVal1");
                session.setAttribute("searchVal1",searchVal1);

                searchVal2 = urlManager.getRequestParameter("searchVal2");
                session.setAttribute("searchVal2",searchVal2);
                flags = flags + '1';
            }

            String grouped = urlManager.getRequestParameter("grouped");
            if (grouped.length() == 0){
                grouped = (String) session.getAttribute("grouped");
            }else {
                session.setAttribute("grouped",grouped);
                flags = flags + '1';
            }

            String pageNo = urlManager.getSecureParameter("pageNo");
            if (pageNo.length() == 0) {
                pageNo = "1";
            }

            final Account cfiAccount =  getAccountHome().findById(new ObjectId(EncryptionHelper.decrypt(sOidAccount,randomKey)));
            final Statement cfiStatement = getStatementHome().findById(new ObjectId(EncryptionHelper.decrypt(sOidStatement,randomKey)));
            final StatementSection cfiRootSection = cfiStatement.getRootSection();

            req.setAttribute(EbppxConstants.CFI_ACCOUNT, cfiAccount);
            req.setAttribute(EbppxConstants.CFI_STATEMENT, cfiStatement);
            req.setAttribute("rootSection", cfiRootSection);
            req.setAttribute("statementSection", cfiRootSection.getSection("DetailsOfChargesSection"));
            req.setAttribute("stmtIndex", EncryptionHelper.decrypt(stmtIndex,randomKey));
            req.setAttribute("phoneNo", phoneNo);
            req.setAttribute("phoneIndex", phoneIndex);
            req.setAttribute("simNo", simNo);
            req.setAttribute("sectionKey", sectionKey);
            req.setAttribute("repeatSectionKey", repeatSectionKey);
            req.setAttribute("flags", flags);
            req.setAttribute("searchVal1", searchVal1);
            req.setAttribute("searchVal2", searchVal2);
            req.setAttribute("searchBy", searchBy);
            req.setAttribute("pageNo", pageNo);
            req.setAttribute("grouped", grouped);
            req.setAttribute("search", search);

//Start Bug Fix : Lendy 20030226
            req.setAttribute("tlcCount", urlManager.getSecureParameter("tlcCount"));
//End Bug Fix : Lendy 20030226

            forwardToMappedUrl(req, res, EbppxConstants.URL_JSP_PARAM);
        } catch (ServletException e) {
            // Just re-throw these
            throw e;
        } catch (Throwable t) {
            throw new ServletException(t.toString(), t);
        }
    }
}